//
//  BarView.m
//  B段项目
//
//  Created by dllo on 15/9/24.
//  Copyright (c) 2015年 昶博. All rights reserved.
//

#import "BarView.h"
#import "PrefixHeader.pch"
#import "FirstScrollView.h"
#import <RESideMenu.h>

@interface BarView () <UIScrollViewDelegate, UISearchBarDelegate>

@property (nonatomic, strong) NSArray *nameArr;
@property (nonatomic, strong) NSArray *buttonArr;

@end

@implementation BarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //添加app名字图片
        self.nameImage = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth * 40 / 375, ScreenHeight * 30 / 667, ScreenWidth * 80 / 375, ScreenHeight * 30 / 667)];
        self.nameImage.image = [UIImage imageNamed:@"maiqi3.png"];
        
        [self addSubview:self.nameImage];

        self.moreImage = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth * 10 / 375, ScreenHeight * 35 / 667, ScreenWidth * 20 / 375, ScreenHeight * 20 / 667)];
        [self.moreImage setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
        [self.moreImage addTarget:self action:@selector(drawer:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:self.moreImage];
        
        [self buttonScrollViewSubviews];
        [self bigScrollViewSubviews];
        [self searchBarSubviews];
        [self searchButtonSubviews];
        
        
    }
    
    return self;
}

- (void)drawer:(UIButton *)button
{
    NSNotificationCenter *drawerCenter = [NSNotificationCenter defaultCenter];
    [drawerCenter postNotificationName:@"drawer" object:nil];
}

- (void)buttonScrollViewSubviews
{
    //添加滚动button下的scrollerView
    self.buttonScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, ScreenHeight * 70 / 667, ScreenWidth * 330 / 375, ScreenHeight * 35 / 667)];
    self.buttonScrollView.backgroundColor = [UIColor whiteColor];
    self.buttonScrollView.contentSize = CGSizeMake(ScreenWidth * 600 / 375, 0);
    self.buttonScrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.buttonScrollView];
    
    //添加滚动button
    
    self.nameArr = @[@"最新", @"新闻", @"评测", @"导购", @"油价变动", @"车辆召回", @"新车上市", @"新技术"];
    for (int i = 0; i < 8; i++) {
        
        self.titleButton = [UIButton buttonWithType:UIButtonTypeSystem];
        self.titleButton.frame = CGRectMake(0 + (ScreenWidth * 76 / 375) * i, ScreenHeight * 5 / 667, ScreenWidth * 76 / 375, ScreenHeight * 30 / 667);
        
        [self.titleButton setTitle:self.nameArr[i] forState:UIControlStateNormal];
        self.titleButton.tintColor = [UIColor blackColor];
        self.titleButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [self.buttonScrollView addSubview:self.titleButton];
        self.titleButton.tag = 100 + i;
        [self.titleButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i == 0) {
            self.tempButton = self.titleButton;
            self.tempButton.tintColor = [UIColor colorWithRed:22 / 255.0 green:188 / 255.0 blue:180 / 255.0 alpha:1];              _tempButton.titleLabel.font = [UIFont systemFontOfSize:15];
        }
    }
}

- (void)bigScrollViewSubviews
{
    //添加tableView下的scrollerView
    self.firstScrollView = [[FirstScrollView alloc] initWithFrame:CGRectMake(0, ScreenHeight * 105 / 667, ScreenWidth , ScreenHeight - ScreenHeight * 105 / 667)];
    self.firstScrollView.backgroundColor = [UIColor whiteColor];
    self.firstScrollView.contentSize = CGSizeMake(ScreenWidth * 8, 0);
    self.firstScrollView.delegate = self;
    self.firstScrollView.pagingEnabled = YES;
    [self addSubview:self.firstScrollView];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    UIButton *button = (UIButton *)[self viewWithTag:100 + self.firstScrollView.contentOffset.x / ScreenWidth ];
    [self buttonClick:button];
    
    if (self.firstScrollView.contentOffset.x >= ScreenWidth * 4) {
        [self.buttonScrollView setContentOffset:CGPointMake(ScreenWidth * 270 / 375, 0) animated:YES];
    }else {
        [self.buttonScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
}

- (void)searchBarSubviews
{
    //添加弹出的SeachBar
    self.search = [[UISearchBar alloc] initWithFrame:CGRectMake(ScreenWidth + 30, ScreenHeight * 35 / 667, ScreenWidth * 180 / 375, ScreenHeight * 32 / 667)];
    self.search.layer.borderWidth = 0;
    self.search.keyboardType = 0;
    self.search.placeholder = @"前沿车新闻一键搜索";
    [self.search setTranslucent:YES];
    
    self.search.searchBarStyle = UISearchBarStyleMinimal;
    self.search.searchResultsButtonSelected = YES;
    //self.backgroundColor = [UIColor blackColor];
    
    [self addSubview:self.search];
    self.search.delegate = self;
    
    //添加seacchBar前面的小汽车图片
    self.carImage = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth, ScreenHeight * 35 / 667, ScreenWidth * 32 / 375, ScreenHeight * 32 / 667)];
    self.carImage.image = [UIImage imageNamed:@"car.png"];
    [self addSubview:self.carImage];
}

- (void)searchButtonSubviews
{
    //添加搜索button
    self.searchbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.searchbutton.frame = CGRectMake(ScreenWidth * 330 / 375, ScreenHeight * 70 / 667, ScreenWidth - ScreenWidth * 330 / 375, ScreenHeight * 35 / 667);
    [self.searchbutton setImage:[UIImage imageNamed:@"sousuo.png"] forState:UIControlStateNormal];
    self.searchbutton.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.searchbutton];
    
    [self.searchbutton addTarget:self action:@selector(searchbuttonClick:) forControlEvents:UIControlEventTouchUpInside];
}

//滑动button点击方法(bigScrollView 随button动)
- (void)buttonClick:(UIButton *)button
{
    self.tempButton.titleLabel.font = [UIFont systemFontOfSize:13];
    self.tempButton.tintColor = [UIColor blackColor];
    
    button.tintColor = [UIColor colorWithRed:22 / 255.0 green:188 / 255.0 blue:180 / 255.0 alpha:1];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    
    self.tempButton = button;
    
    [self.firstScrollView setContentOffset:CGPointMake((button.tag - 100) * ScreenWidth , 0) animated:NO];
}

//搜索button点击方法
- (void)searchbuttonClick:(UIButton *)button
{
    button.selected = !button.selected;
    
    [UIView animateWithDuration:0.8 animations:^{
        if (button.selected == 1) {
            self.search.frame = CGRectMake(ScreenWidth * 160 / 375, ScreenHeight * 35 / 667, ScreenWidth * 210 / 375, ScreenHeight * 25 /667);
            self.carImage.frame = CGRectMake(ScreenWidth * 130 / 375, ScreenHeight * 35 / 667, ScreenWidth * 32 / 375, ScreenHeight * 32 / 667);
            [self.searchbutton setImage:[UIImage imageNamed:@"cha.png"] forState:UIControlStateNormal];
        }
        else
        {
            self.search.frame = CGRectMake(ScreenWidth + 32, ScreenHeight * 35 / 667, ScreenHeight * 180 / 375, ScreenHeight * 32 / 667);
            self.carImage.frame = CGRectMake(ScreenWidth, ScreenHeight * 35 / 667, ScreenWidth * 32 / 375, ScreenHeight * 32 / 667);
            [self.searchbutton setImage:[UIImage imageNamed:@"sousuo.png"] forState:UIControlStateNormal];
        }
    }];
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
